package com.hs.service.impl;

import com.hs.common.Password;
import com.hs.entity.TbUser;
import com.hs.mapper.TbUserMapper;
import com.hs.service.SequenceService;
import com.hs.service.UserService;
import com.hs.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private TbUserMapper userMapper;

    @Autowired
    private SequenceService sequenceService;


    @Override
    public List<TbUser> findUserListByRoleId(Integer roleId) {
        return userMapper.findUserListByRoleId(roleId);
    }

    /*登录查询
    * */
    @Override
    public TbUser login(String account) {
        return userMapper.selectByUsername(account);
    }

    @Override
    public List<TbUser> findUserList() {
        return userMapper.findUserList();
    }

    @Override
    public TbUser queryForForgetPassword(String account, String realname, String email, String roleid, Integer gender) {
        return userMapper.queryForForgetPassword(account,realname,email,roleid,gender);
    }

    @Override
    public int changeForForgetPassword(String password, Integer id) {
        return userMapper.changeForForgetPassword(password,id);
    }

    @Override
    public Integer createAccount(String realName,String password,Integer roleId) {
        if (password==null){
            password="";
        }
        String number = "";
        String account = "";
        if (roleId==2){
            password= Password.teacherDefaultPassword();
            number=sequenceService.nextValue("tea_seq",roleId);
            account=number;
        }else if(roleId==3){
            password=Password.studentDefaultPassword();
            number=sequenceService.nextValue("stu_seq",roleId);
            account=number;
        }
        return userMapper.createAccount(realName, MD5Utils.getMD5(password),number,account,roleId);
    }

    @Override
    public TbUser queryById(Integer id) throws SQLException {
        return userMapper.queryById(id);
    }

    @Override
    public Integer updateUserForStudent(Map<String, String[]> map) throws SQLException {
        return userMapper.updateUserForStudent(map);
    }

    @Override
    public Integer updatePasswordById(String md5, Integer id) {
        return userMapper.updatePasswordById(md5,id);
    }

    @Override
    public Integer countUser(String roleId) {
        return userMapper.countUser(roleId);
    }
}
